package com.itheima.test02;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        File file;
        //判断
        while (true) {
            System.out.println("请输入上传文件的路径:");
            String s = sc.next();
            file = new File(s);
            String[] split = file.getName().split("\\.");
            if (file.exists() && "jpg".equals(split[split.length-1])) {
                break;
            }
            System.out.println("请输入正确的图片文件路径，请重新输入");
        }

        //上传图片

        //创建客户端对象
        Socket socket = new Socket("127.0.0.1",10086);
        //获取网络输出流
        OutputStream os = socket.getOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(os);

        //本地输入流---用上面输入的地址file对象为参数
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int b;
        //读取---输出
        while ((b = bis.read())!=-1){
            bos.write(b);
        }
        bos.flush();
        bis.close();
        //暂时停止网络输出流---给结束标记
        socket.shutdownOutput();

        //接收
        InputStream is = socket.getInputStream();
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = br.readLine();
        System.out.println("服务器的反馈："+line);

        br.close();
        bos.close();
        socket.close();


    }
}
